using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;

using EntLibEC.Biz;
using EntLibEC.Biz.Sale;

namespace EntLibEC.View.Sale
{
	/// <summary>
	/// Summary description for SaleGiftRelateMoneySearch.
	/// </summary>
	public partial class SaleGiftRelateMoneySearch : PageBase
	{

		protected EntLibEC.View.Cmn.BiStatus ucStatus;
		protected EntLibEC.View.Cmn.StockProduct ucProduct;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			DataGrid1.PageSize = AppConst.PageSize;
			DataGrid1.PagerStyle.PageButtonCount = AppConst.PageButtonCount;

			btnAdd.Attributes["onclick"]= "javascript:openWindowS2('SaleGiftRelateMoneyOpt.aspx?opt=new');return false;";

			txtDateFromCreate.Attributes["onclick"]= "javascript:calendar()";
			txtDateToCreate.Attributes["onclick"]= "javascript:calendar()";

			if ( !Page.IsPostBack)
			{
				ucStatus.BindStatus(true);
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);

		}
		#endregion

		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
			BindGrid();
		}
		private bool CheckForm()
		{
			if ( txtDateFromCreate.Text.Trim() != "" && !Util.IsDate(txtDateFromCreate.Text.Trim()))
			{
				Assert(lblMessage, "Date(From) Create format error", -1);
				return false;
			}
			if ( txtDateToCreate.Text.Trim() != "" && !Util.IsDate(txtDateToCreate.Text.Trim()))
			{
				Assert(lblMessage, "Date(To) Create format error", -1);
				return false;
			}
			return true;
		}

		private void BindGrid()
		{
			if ( !CheckForm() )
				return;
			
			Hashtable ht = new Hashtable(10);

			if ( ucProduct.ProductSysNo != AppConst.IntNull )
				ht.Add("ProductSysNo", ucProduct.ProductSysNo );
			if (txtDateFromCreate.Text.Trim() != "")
				ht.Add("DateFromCreate", txtDateFromCreate.Text.Trim());
			if ( txtDateToCreate.Text.Trim() != "")
				ht.Add("DateToCreate", txtDateToCreate.Text.Trim());

			if ( ucStatus.StatusSysNo != AppConst.IntNull )
				ht.Add("Status", ucStatus.StatusSysNo);

			DataSet ds = ConvertDs(SaleGiftRelateMoneyManager.GetInstance().GetDs(ht));

			base.SetDataGridPage(ds, DataGrid1);

			DataGrid1.DataSource = ds;
			DataGrid1.DataBind();
			
			int rows = 0;
			if ( Util.HasMoreRow(ds) )
				rows = ds.Tables[0].Rows.Count;
			Assert(lblMessage, rows.ToString() + " record(s)", 1);
		}

		private DataSet ConvertDs(DataSet ds)
		{
			if ( !Util.HasMoreRow(ds))
				return ds;

			ds.Tables[0].Columns.Add("StatusName");
			ds.Tables[0].Columns.Add("Operation");
			ds.Tables[0].Columns.Add("CreateCombo");
			ds.Tables[0].Columns.Add("MoneyStartCvt");
			ds.Tables[0].Columns.Add("MoneyEndCvt");


			foreach( DataRow dr in ds.Tables[0].Rows )
			{
				dr["StatusName"] = AppEnum.GetBiStatus(Util.TrimIntNull(dr["Status"]));

				dr["CreateCombo"] = Util.TrimNull(dr["CreateName"]) + "[" + Util.TrimDateNull(dr["CreateTime"]).ToString(AppConst.DateFormatLong) + "]";
				dr["MoneyStartCvt"] = Util.TrimDecimalNull(dr["MoneyStart"]).ToString(AppConst.DecimalFormat);
				dr["MoneyEndCvt"] = Util.TrimDecimalNull(dr["MoneyEnd"]).ToString(AppConst.DecimalFormat);

				dr["Operation"] = "<a href=\"javascript:openWindowS2('SaleGiftRelateMoneyOpt.aspx?sysno=" + dr["sysno"].ToString() + "&opt=update')\">Update</a>";
			}
			return ds;
		}

		private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if ( !base.CheckAccess((int)AppEnum.Privilege.SaleGiftRelateMoney, false, lblMessage) )
				return;

			if ( e.CommandName == "delete" )
			{
			
				int sysno = (int)DataGrid1.DataKeys[(int)e.Item.ItemIndex];
				try
				{
					SaleGiftRelateMoneyManager.GetInstance().Delete(sysno);
					BindGrid();
					Assert(lblMessage, "delete ok", 1);

				}
				catch(BizException exp)
				{
					Assert(lblMessage, exp.Message, -1);
				}
				catch(Exception exp)
				{
					Assert(lblMessage, "delete failed, see log", -1);
					ErrorLog.GetInstance().Write(exp.ToString());
				}
			}
		}
	}
}
